package cn.btks.zuulserver.feign.service.impl;

import cn.btks.commonserver.result.ResultState;
import cn.btks.zuulserver.dto.User;
import cn.btks.zuulserver.feign.service.backmanage.BackManageClient;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Author lijun
 * @Date 2021/1/11 19:48
 */
@Slf4j
@Service
public class UserClientImpl implements UserDetailsService {

    @Autowired
    BackManageClient backManageClient;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        ResultState resultState = backManageClient.findByUserName(username);
        Object data = resultState.getData();
        if (data == null) {
            log.error("用户名不存在");
            backManageClient.recordLoginCount(username, "未知用户");
            throw new UsernameNotFoundException("用户名不存在");
        }
        User user = JSON.parseObject(JSON.toJSONString(data), User.class);
//        User user = (User)data;
        return user;
    }
}
